package com.atfusu.controller;

import com.atfusu.bean.entity.UserInfo;
import com.atfusu.bean.vo.LoginVo;
import com.atfusu.bean.vo.RegisterVo;
import com.atfusu.result.Result;
import com.atfusu.result.ResultCodeEnum;
import com.atfusu.service.UserInfoService;
import com.atfusu.util.MD5;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/12/2
 * Description:
 **/
@Controller
@RequestMapping("/userInfo")
@ResponseBody
public class UserInfoController {

    @DubboReference
    private UserInfoService userInfoService;

    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpSession session) {
        //生成验证码，模拟验证码的发送与校验
        int number = (int) (Math.random() * 9000) + 1000;
        String code = "" + number;
        //将验证码存入会话域内，待会用于注册验证
        session.setAttribute("code", code);
        return Result.ok(code);
    }

    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpSession session) {
        String nickName = registerVo.getNickName();
        String password = registerVo.getPassword();
        String phone = registerVo.getPhone();
        String code = registerVo.getCode();
        //非空校验
        if (StringUtils.isEmpty(nickName) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(code)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //验证码校验
        if (!code.equals(session.getAttribute("code"))) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //手机号是否重复
        UserInfo user = userInfoService.getUserByPhone(phone);
        if (user != null) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }

        //将数据添加到数据库
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(nickName);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setPhone(phone);
        userInfo.setStatus(1);
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpSession session) {
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        //①数据的非空校验
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //②判断用户是否存在
        UserInfo userInfo = userInfoService.getUserByPhone(phone);
        if (userInfo == null) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //③校验密码
        if (!MD5.encrypt(password).equals(userInfo.getPassword())) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //④校验是否被锁定
        if (userInfo.getStatus() != 1) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //④将用户存入session域
        session.setAttribute("userInfo", userInfo);
        //⑤将数据返回给客户端，用于登录状态的保持
        Map<String, Object> map = new HashMap<>();
        map.put("nickName", userInfo.getNickName());
        map.put("phone", userInfo.getPhone());
        return Result.ok(map);
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("userInfo");

        return Result.ok();
    }
}
